package com.hjx.movie.core.system.controller.commons;

import com.hjx.movie.common.utils.Result;

import com.hjx.movie.core.system.service.commons.file.FileService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/17 - 14:53
 */
@RestController
public class FileController {
    @Value("${file.disk}")
    private String disk;

    @Value("${file.path}")
    private String path;

//    @RequestMapping("fileUpload")
//    public Result fileUpload(@RequestHeader("id")Long id, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
////            final String realPath = request.getServletContext().getRealPath("/upload");
//        String realPath = disk+ File.separator + path;
//        FileUpAndDownloadService.fileUpload(username,multipartFile,realPath);
//        return Result.success("文件上传成功");
//    }


    @RequestMapping("download")
    public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("fileName") String fileName) {
        //获取文件所在的路径
//        String path = request.getServletContext().getRealPath("/upload");
        String path = "D:" + File.separator + "upload";
        File loadFile = new File(path + File.separator + fileName);
        //将文件名编码 防止乱码
        try {
            String encodeName = FileService.getFileName(request, fileName);

            //设置响应头信息
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", encodeName);
            return new ResponseEntity<>(FileUtils.readFileToByteArray(loadFile), headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("importUser")
    public Result importUser(Map<String, Object> map, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//            final String realPath = request.getServletContext().getRealPath("/upload");
        String realPath = "D:" + File.separator + "upload";
        String path = FileService.fileUpload(multipartFile, realPath);
        //TODO 从文件中将数据导入数据库

        return Result.success("文件导入成功");
    }
}
